import {AbilityModel} from "../../controller/model/AbilityModel";
import Configer from "../../frame/apiModel/config/Configer";
import {FinalKey} from "../../constant/FinalConstant";
import {UnitController} from "../../controller/UnitController";
import {Logger} from "../../frame/apiModel/config/Logger";
import {UnitModel} from "../../frame/apiModel/adapterModel/model3/UnitModel";
import {ItemModel} from "../../frame/apiModel/adapterModel/model3/ItemModel";
import {PlayerModel} from "../../frame/apiModel/adapterModel/model4/PlayerModel";
import {LocModel} from "../../frame/apiModel/base/LocModel";
import {PlayerFactory} from "../../frame/PlayerFactory";
import {DataRouterUtil} from "../../utils/router/DataRouterUtil";
import {CodeUtil} from "../../frame/codeAdapter/CodeUtil";
import {AttributeEnum, UnitType, WeightIDData} from "../../frame/dataSource/DataModel";
import {Util} from "../../utils/Util";
import {AbilityUtil} from "./AbilityUtil";
import UnitUtil from "../../utils/UnitUtil";
import {DataRouter} from "../../frame/dataSource/DataRouter";
import {EffectModel} from "../../frame/apiModel/adapterModel/model2/EffectModel";
import {ModelFactory} from "../../frame/apiModel/adapterModel/model5/ModelFactory";
import {TimeControllerOpt} from "../../controller/model/ControllerOpt";
import {TimeController} from "../../controller/TimeController";
import MathUtil from "../../frame/codeAdapter/MathUtil";
import {GlobalEnv} from "../../utils/GlobalEnv";
import {TaskController} from "../../controller/TaskController";
import {ChangeUnitType} from "../../type/Type";
import {ModelType} from "../../frame/apiModel/adapter/adapterInterface/model5/IModelFactory";

export default class EvolutionSkillModel extends AbilityModel {
    abilityId: string = Configer.getFinal(FinalKey.evolutionPetSkill) as string;

    actionFun = (activeUnit: UnitModel, activeItem: ItemModel, targetUnit: UnitModel, targetItem: ItemModel, activePlayer: PlayerModel, targetPlayer: PlayerModel, spellId: string, spellLoc: LocModel)  => {
        let success;
        this.evolutionPetSkill(activeUnit);
        return success;
    }

    evolutionPetSkill(activeUnit: UnitModel) {
        const player = PlayerFactory.getInstance().getUnitOwner(activeUnit);

        const hero = GlobalEnv.getHero(player);
        const isVaild = hero;
        if (isVaild) {
        } else {
            Logger.toastProduct('请选择人物后再操作宠物!', player);
            return;
        }

        const delay = 3;

        const unitId = activeUnit.strId;
        const petData = DataRouterUtil.getPetDataByGetId(unitId);
        if (petData) {
        } else {
            Logger.toastProduct('进化单位没有进行相关配置!', player);
            return;
        }
        let ret
        const evolution_condition = petData && petData.evolution_condition;
        const conditionArr = CodeUtil.getArrBySplit(evolution_condition);
        const tempRet = TaskController.getInstance().checkConditions(activeUnit, conditionArr, '', player)
        ret = tempRet.success;

        if (ret) {
            const evolutionIds = petData.evolution;
            const evolutionIdArr = CodeUtil.getArrBySplit(evolutionIds);
            const evolutionWeights = petData.evolution_weight;
            const evolutionWeightArr = CodeUtil.getArrBySplit(evolutionWeights);

            const datas: WeightIDData[] = [];

            for (let i = 0; i < evolutionIdArr.length; i++) {
                const data: WeightIDData = { id: evolutionIdArr[i], weight: DataRouterUtil.getNumByArr(evolutionWeightArr, i), probability: 0 };
                datas.push(data);
            }

            const ret = DataRouterUtil.getDataModelByWeight(player, datas, 1);
            let getId = ret && ret[0] && ret[0].id;

            if (getId) {
            } else {
                getId = unitId;
                Logger.toastProduct('进化失败!', player);
            }
            const callBack = () => {
                UnitController.getInstance().changeUnit(activeUnit, getId, ChangeUnitType.other, false);
            }
            const delayTimerOpt = new TimeControllerOpt(undefined, callBack, 1);
            TimeController.getInstance().startTimer({time: delay, opt: delayTimerOpt});
        }
    }
}